<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/24
 * Time: 15:30
 */
namespace app\api\controller;

use app\api\model\Address as Addresss;
use app\common\Common;
use think\Request;
use think\Session;

class Address extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        try {
            if ($_COOKIE['PHPSESSID']) {
                session_id($_COOKIE['PHPSESSID']);
                $uid = Session::get('uid',$this->config_session['prefix']);
                $list = Addresss::all(function ($query) use ($uid) {
                    $query->where('uid',$uid)
                        ->order('isdefault desc,id desc');
                });
                return json($list);
            }else{
                //重新登录
                return abort(404, 'PHPSESSID不存在');
            }
        } catch (\Exception $e) {
            // 捕获异常并转发为HTTP异常
            return abort(404, $e->getMessage());
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        try {
            if ($_COOKIE['PHPSESSID']) {
                session_id($_COOKIE['PHPSESSID']);
                $uid = Session::get('uid',$this->config_session['prefix']);
                $data   = $request->param();
                $validate = validate('Address');
                if (!$validate->check($data)) {
                    abort(409,"invalid parameter:".$validate->getError());
                }else{
                    $data['uid'] = $uid;
                    $result = Addresss::create($data);
                    return json($result);
                }
            }else{
                //重新登录
                return abort(404, 'PHPSESSID不存在');
            }
        } catch (\Exception $e) {
            // 捕获异常并转发为HTTP异常
            return abort(404, $e->getMessage());
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        try {
            if ($_COOKIE['PHPSESSID']) {
                session_id($_COOKIE['PHPSESSID']);
                $uid = Session::get('uid',$this->config_session['prefix']);
                $data = Addresss::get(['id'=>$id,'uid'=>$uid]);
                if ($data) {
                    return json($data);
                } else {
                    return abort(404, '地址不存在');
                }
            }else{
                //重新登录
                return abort(404, 'PHPSESSID不存在');
            }
        } catch (\Exception $e) {
            // 捕获异常并转发为HTTP异常
            return abort(404, $e->getMessage());
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        try{
            if ($_COOKIE['PHPSESSID']) {
                session_id($_COOKIE['PHPSESSID']);
                $uid = Session::get('uid',$this->config_session['prefix']);
                $data   = $request->param();
                $validate = validate('Address');
                if (!$validate->check($data)) {
                    return abort(409,"invalid parameter:".$validate->getError());
                }else{
                    if(Addresss::get($id)){
                        $result = Addresss::update($data, ['id' => $id]);
                        return json($result);
                    }else{
                        return abort(404, '地址不存在');
                    }
                }
            }else{
                //重新登录
                return abort(404, 'PHPSESSID不存在');
            }
        } catch (\Exception $e) {
            // 捕获异常并转发为HTTP异常
            return abort(404, $e->getMessage());
        }

    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        try{
            if ($_COOKIE['PHPSESSID']) {
                session_id($_COOKIE['PHPSESSID']);
                $result = Addresss::destroy($id);
                return json($result);
            }else{
                //重新登录
                return abort(404, 'PHPSESSID不存在');
            }
        } catch (\Exception $e) {
            // 捕获异常并转发为HTTP异常
            return abort(404, $e->getMessage());
        }
    }
}